package zhoug.chart.core.styleconfig;

import android.graphics.Color;

import com.github.mikephil.charting.charts.HorizontalBarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.RadarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.interfaces.datasets.IDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import zhoug.chart.Chart2Sdk;
import zhoug.chart.MPChartConfig;
import zhoug.chart.charts.IMPChart;
import zhoug.chart.utils.MPChartUtils;
import zhoug.logging.Logger;

import androidx.annotation.NonNull;

/**
 * @Author: zhoug
 * @Date: 2024-02-03
 * @Description:
 */
public class MPChartStyleDefaultUtils {
    private static boolean DEBUG = Chart2Sdk.isDebug();

    private static final String TAG = ">>>ChartStyleDefault";

    /**
     * 图表dataSet默认样式
     * @param dataSet
     */
    public static void initDataSet(IDataSet<?> dataSet){
        dataSet.setDrawValues(false);
        dataSet.setHighlightEnabled(true);
        dataSet.setValueTextSize(MPChartConfig.VALUE_TEXT_SIZE);
        dataSet.setValueTextColor(MPChartConfig.VALUE_TEXT_COLOR);
    }

    /**
     * 柱状图的默认样式
     *
     * @param barDataSet
     */
    public static void initBarDataSet(BarDataSet barDataSet) {
        initDataSet(barDataSet);
        barDataSet.setHighLightAlpha(0);
        barDataSet.setForm(Legend.LegendForm.SQUARE);

    }

    /**
     * 曲线图的默认样式
     *
     * @param lineDataSet
     */
    public static void initLineDataSet(LineDataSet lineDataSet) {
        initDataSet(lineDataSet);
        int color = lineDataSet.getColor();
        lineDataSet.setLineWidth(MPChartConfig.LINE_WIDTH);//折线宽度
        lineDataSet.setDrawCircles(true);
        lineDataSet.setCircleColor(color);
        lineDataSet.setCircleHoleColor(Color.WHITE);//默认white
        lineDataSet.setCircleRadius(MPChartConfig.LINE_CIRCLE_RADIUS);//点的半径
        lineDataSet.setDrawCircleHole(true);
        lineDataSet.setCircleHoleRadius(MPChartConfig.LINE_CIRCLE_HOLE_RADIUS);
        lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        lineDataSet.setDrawFilled(true);
        lineDataSet.setFillColor(color);
        lineDataSet.setFillAlpha(MPChartConfig.LINE_FILL_ALPHA);
        lineDataSet.setForm(Legend.LegendForm.LINE);
    }

    /**
     * 饼图的默认样式
     * @param pieDataSet
     */
    public static void initPieDataSet(PieDataSet pieDataSet){
        initDataSet(pieDataSet);
        pieDataSet.setSliceSpace(MPChartConfig.PIE_SLICE_SPACE);
        pieDataSet.setDrawValues(true);
        //label显示在图外面
        pieDataSet.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        //value显示在图外面
        pieDataSet.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        //可变线段长度
        pieDataSet.setValueLineVariableLength(true);
        //线段开始位置对于饼图环里面的偏移量
        pieDataSet.setValueLinePart1OffsetPercentage(75);
        //靠近圆环部分的线段的占比:越大距离图形越远
        pieDataSet.setValueLinePart1Length(0.5f);
        //靠近value部分的线段的占比
        pieDataSet.setValueLinePart2Length(0.3f);
        //使用饼图切片颜色作为对应的线条颜色
        pieDataSet.setUsingSliceColorAsValueLineColor(true);
    }

    /**
     * 雷达图的默认样式
     * @param radarDataSet
     */
    public static void initRadarDataSet(RadarDataSet radarDataSet) {
        initDataSet(radarDataSet);
        radarDataSet.setDrawValues(false);

    }
    /**
     * x轴最小值和最大值设置
     * @param xAxis
     * @param lineData
     * @param attachAxis
     */
    public static void initXAxisMinMax(XAxis xAxis, LineData lineData, boolean attachAxis) {
        if (lineData != null) {
            ILineDataSet dataSet = lineData.getDataSetByIndex(0);
            int xCount = dataSet != null ? dataSet.getEntryCount() : 0;
            if (xCount > 0) {
                if (attachAxis) {
                    xAxis.setAxisMinimum(1);
                    xAxis.setAxisMaximum(xCount);
                } else {
                    xAxis.setAxisMinimum(0.5f);
                    xAxis.setAxisMaximum(xAxis.getAxisMinimum() + xCount);
                }
            }
        }
    }

    /**
     * x轴最小值和最大值设置
     * @param xAxis
     * @param barData
     */
    public static void initXAxisMinMax(XAxis xAxis, BarData barData,boolean isOverLap) {
        if (barData != null) {
            IBarDataSet dataSet = barData.getDataSetByIndex(0);
            int xCount = dataSet != null ? dataSet.getEntryCount() : 0;
            if (xCount > 0) {
                int dataSetCount = barData.getDataSetCount();
                boolean multiBar = dataSetCount > 1 && !isOverLap;
                xAxis.setCenterAxisLabels(multiBar);
                //并排时居中
                if (multiBar) {
                    xAxis.setAxisMinimum(1);
                } else {
                    xAxis.setAxisMinimum(0.5f);
                }
                xAxis.setAxisMaximum(xAxis.getAxisMinimum() + xCount);
            }
        }
    }


    /**
     * x轴最小值和最大值设置
     * @param xAxis
     * @param combinedData
     * @param attachAxis
     */
    public static void initXAxisMinMax(XAxis xAxis, CombinedData combinedData, boolean attachAxis,boolean isOverLap) {
        if (combinedData != null) {
            BarData barData = combinedData.getBarData();
            if (barData != null && barData.getDataSetCount() > 0) {
                initXAxisMinMax(xAxis, barData,isOverLap);
            } else {
                LineData lineData = combinedData.getLineData();
                initXAxisMinMax(xAxis, lineData, attachAxis);
            }

        }
    }

    /**
     * 设置混合图表的缩放和柱子的宽度
     * @param chart
     * @param data
     * @param styleConfig
     */
    public static void initCombinedWidthAndScale(@NonNull IMPChart chart, CombinedData data, IMPCombinedStyleConfig styleConfig) {
        BarData barData = data.getBarData();
        LineData lineData = data.getLineData();
        if (barData != null) {
            MPChartStyleDefaultUtils.initBarWidthAndScale(chart, barData, styleConfig);
        } else if (lineData != null) {
            MPChartStyleDefaultUtils.initLineScale(chart, lineData, styleConfig);
        }
    }

    /**
     * 设置柱子宽度和缩放
     *
     * @param barData
     */
    public static void initBarWidthAndScale(@NonNull IMPChart chart, BarData barData, IMPBarStyleConfig barStyle) {
        if (barData != null && barStyle != null) {
            int oneScreen = barStyle.getOneScreen();
            IBarDataSet dataSet = barData.getDataSetByIndex(0);
            int xCount = dataSet != null ? dataSet.getEntryCount() : 0;
            int dataSetCount = barData.getDataSetCount();
            float scaleX = 0;
            if (dataSetCount > 1 && !barStyle.isOverlap()) {
                //并排分组
                if (barStyle.isSpaceIsPercent()) {
                    float barWidth = 1 / ((1 + barStyle.getBarSpace()) * dataSetCount + barStyle.getGroupSpace());
                    float groupSpace = barWidth * barStyle.getGroupSpace();
                    float barSpace = barWidth * barStyle.getBarSpace();
                    // 0.1 0.1 0.6 0.1 0.1
                    barData.setBarWidth(barWidth);
                    float xMin = barData.getXMin();
                    barData.groupBars(xMin, groupSpace, barSpace);
                    if (DEBUG) {
                        Logger.d(TAG, "groupSpace:" + groupSpace + ",barWidth:" + barWidth + ",barSpace:" + barSpace);
                        Logger.d(TAG, (barWidth + barSpace) * dataSetCount + groupSpace + "");
                    }
                    if (oneScreen > 0) {
                        scaleX = barStyle.getBarWidth() / barWidth * (xCount * 1.0f / oneScreen);
                    }
                } else {
                    float groupSpace = barStyle.getGroupSpace();
                    float barSpace = barStyle.getBarSpace();
                    float barWidth = MPChartUtils.calculateBarWidth(dataSetCount, groupSpace, barSpace);
                    // 0.1 0.1 0.6 0.1 0.1
                    barData.setBarWidth(barWidth);
                    float xMin = barData.getXMin();
                    barData.groupBars(xMin, groupSpace, barSpace);
                    if (DEBUG) {
                        Logger.d(TAG, "groupSpace:" + groupSpace + ",barWidth:" + barWidth + ",barSpace:" + barSpace);
                        Logger.d(TAG, (barWidth + barSpace) * dataSetCount + groupSpace + "");
                    }
                    if (oneScreen > 0) {
                        scaleX = xCount * 1.0f / oneScreen;
                    }
                }


            } else {
                //柱子宽度
                barData.setBarWidth(barStyle.getBarWidth());
                if (oneScreen > 0) {
                    scaleX = xCount * 1.0f / oneScreen;
                }

            }
            if (scaleX > 0) {
                if(chart instanceof HorizontalBarChart){
                    chart.getViewPortHandler().setMaximumScaleY(scaleX);
                    if (barStyle.isAutoScaleMax()) {
                        chart.zoom(1, scaleX, 0, 0);
                    }
                    if (DEBUG) {
                        Logger.d(TAG, "scaleY:" + scaleX);
                    }
                }else{
                    chart.getViewPortHandler().setMaximumScaleX(scaleX);
                    if (barStyle.isAutoScaleMax()) {
                        chart.zoom(scaleX, 1, 0, 0);
                    }
                    if (DEBUG) {
                        Logger.d(TAG, "scaleX:" + scaleX);
                    }
                }

            } else {
                chart.zoomNotScale();
            }
        }
    }

    /**
     * 设置曲线图缩放
     * @param chart
     * @param lineData
     * @param styleConfig
     */
    public static void initLineScale(@NonNull IMPChart chart, LineData lineData, IMPLineStyleConfig styleConfig) {
        if (lineData != null) {
            ILineDataSet dataSet = lineData.getDataSetByIndex(0);
            int xCount = dataSet != null ? dataSet.getEntryCount() : 0;
            int oneScreen = styleConfig.getOneScreen();
            if (oneScreen > 0 && xCount > 0) {
                float scaleX = xCount * 1.0f / oneScreen;
                chart.getViewPortHandler().setMaximumScaleX(scaleX);
                if (styleConfig.isAutoScaleMax()) {
                    chart.zoom(scaleX, 1, 0, 0);
                }
                if (DEBUG) {
                    Logger.d(TAG, "zoom:scaleX=" + scaleX);
                }
            } else {
                chart.zoomNotScale();
            }

        }
    }

}
